<?php
set_time_limit(0);
$API_KEY = 'YOUR_API_KEY';

// 设置请求的 URL 和参数
$url = 'https://api.openai.com/v1/chat/completions';
$messagesArr = array(
    'role'=> 'user',
    'content' => $_POST['prompt']
);

$data = array(
    'model' => 'gpt-3.5-turbo',
    'messages' => array($messagesArr),
    'temperature' => 0.7
);
$data_string = json_encode($data);

// 发送 HTTP POST 请求到 OpenAI API
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $API_KEY,
    'OpenAI-Organization: org-BJAv684i9XTcJD1dbdh9H5jt'
));

$response = curl_exec($ch);
// print_r($response);die;

if(curl_error($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    $result = json_decode($response, true);
    $content = $result['choices'][0]['message']['content'];
    echo htmlentities($content);
}

curl_close($ch);
